package com.kang.servlet;

import com.alibaba.fastjson.JSON;
import com.kang.dao.impl.water_purifierDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by lenovo on 2018/8/6.
 */

public class findWaterServlet extends HttpServlet {
    int page=0; //当前查询第几页的数据
    String name="";//要查询的名字
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        water_purifierDaoImpl wp=new water_purifierDaoImpl();

        //查询条件:有name这个值传过来  表示用户点了一下查询按钮
        if(request.getParameter("name")!=null){
            name=request.getParameter("name");
        }

        //一页展示多少数据
        int size=3;

        //让页面传来1 或者 -1   实现上一页  下一页点击   Turn翻页传来的数据
        if(request.getParameter("Turn")!=null){

            page+=Integer.parseInt(request.getParameter("Turn"));
        }else {
            //如果没有翻页 就把当前页码清零
           page=0;
        }
        if(request.getParameter("page")!=null){
            page=Integer.parseInt(request.getParameter("page"));
        }


        //一共有多少数据

        int count=wp.findMaxCount(name);
        //最大页码
        int maxPage=(count+size-1)/size;
        //如果当前页码大于最大页码
        if(page>=maxPage)
        page=maxPage-1;
        if(page<0)
            page=0;
        String json = JSON.toJSONString(wp.findWater(name,page*size,size));
        out.print(json);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
}
